use crate::game_state::GameState;
use crate::scenes::button::{click_check, hover_check, in_button};
use crate::UI_BOTTOM_LAYER;
use rusty_engine::game::Engine;
use rusty_engine::prelude::{TEXT_DEFAULT_FONT_SIZE, TEXT_DEFAULT_LAYER};
use rusty_engine::RIGHT;

pub fn logic(engine: &mut Engine, game_state: &mut GameState) {

    // 初始化逻辑
    if game_state.main_menu_init == false {
        init_main_menu(engine, game_state);
        game_state.main_menu_init = true;
    }

    // 点击检测
    click_check(engine, game_state, "start_single_button");
    click_check(engine, game_state, "start_double_button");
    
    // 悬停检测
    hover_check(engine, "start_single_button");
    hover_check(engine, "start_double_button");

    // 非悬停检测
    if in_button(engine, "start_single_button") == false {
        if let Some(button) = engine.sprites.get_mut("start_single_button") {
            button.scale = 0.75; // 按钮大小恢复
        }
    }
    if in_button(engine, "start_double_button") == false {
        if let Some(button) = engine.sprites.get_mut("start_double_button") {
            button.scale = 0.75; // 按钮大小恢复
        }
    }

    // 如果没有在播放音乐，播放音乐
    if engine.audio_manager.music_playing() == false {
        engine.audio_manager.play_music("music/Classy 8-Bit.ogg", 0.3);
    }
}

fn init_main_menu(engine: &mut Engine, game_state: &mut GameState) {

    // 将其他界面初始化设置为false，避免发生错误
    game_state.game_single_init = false;
    game_state.game_over_init = false;
    game_state.game_double_init = false;

    // 停止播放音乐
    engine.audio_manager.stop_music();
    // 清除元素
    engine.sprites.clear();
    engine.texts.clear();

    // 播放音乐
    engine.audio_manager.play_music("music/Classy 8-Bit.ogg", 0.3);

    // 添加单人模式按钮
    let start_button = engine.add_sprite("start_single_button", "sprite/button/button_blue.png");
    start_button.rotation = RIGHT;
    start_button.translation.y = -30.0;
    start_button.scale = 0.75;
    start_button.layer = UI_BOTTOM_LAYER;
    start_button.collider_dirty = true;
    // 单人模式按钮文本
    let start_game_text = engine.add_text("start_single_game_text","SINGLE GAME");
    start_game_text.translation.y = - 30.0;
    start_game_text.layer = TEXT_DEFAULT_LAYER;
    start_game_text.font_size = TEXT_DEFAULT_FONT_SIZE;

    // 添加双人模式按钮
    let start_button = engine.add_sprite("start_double_button", "sprite/button/button_blue.png");
    start_button.rotation = RIGHT;
    start_button.translation.y = -230.0;
    start_button.scale = 0.75;
    start_button.layer = UI_BOTTOM_LAYER;
    start_button.collider_dirty = true;
    // 双人模式按钮文本
    let start_game_text = engine.add_text("start_double_game_text","DOUBLE GAME");
    start_game_text.translation.y = - 230.0;
    start_game_text.layer = TEXT_DEFAULT_LAYER;
    start_game_text.font_size = TEXT_DEFAULT_FONT_SIZE;

    // 标题文本
    let title_text = engine.add_text("title_text","Ultimate Racer");
    title_text.translation.y = 180.0;
    title_text.layer = TEXT_DEFAULT_LAYER;
    title_text.font_size = TEXT_DEFAULT_FONT_SIZE * 4.0;

    // 操作说明
    let help_text = engine.add_text("help_text", "操作说明:      \n- WASD控制1P移动\n- ↑↓←→控制2P移动\n- 按P暂停游戏   ");
    help_text.translation.x = -450.0;
    help_text.translation.y = -20.0;
    help_text.layer = TEXT_DEFAULT_LAYER;
    help_text.font = "font/HYPixel11pxU-2.ttf".to_string();
    help_text.font_size = TEXT_DEFAULT_FONT_SIZE;

    // 游戏说明
    let game_text = engine.add_text("game_text", "游戏说明:              \n- 金币: 拾取可以提升速度  \n- 爱心: 拾取可以增加生命值\n- 分数: 受距离和金币数影响");
    game_text.translation.x = -400.0;
    game_text.translation.y = -220.0;
    game_text.layer = TEXT_DEFAULT_LAYER;
    game_text.font = "font/HYPixel11pxU-2.ttf".to_string();
    game_text.font_size = TEXT_DEFAULT_FONT_SIZE;
}


